//
// Created by guanyicheng on 2022/3/9.
//
#include "../../common.h"

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode h = ListNode();
        h.next = head;
        ListNode *index = &h;
        while (index->next != nullptr) {
            if (index->next->val == val) {
                auto temp = index->next;
                index->next = index->next->next;
                delete temp;
            } else {
                index = index->next;
            }
        }
        head = h.next;
        return head;
    }
};




